<?php
global $_GPC, $_W;

$GLOBALS['frames'] = $this->getMainMenu2();

$info=pdo_get('zhtc_qrcode',array('id'=>$_GPC['id']));

$cardlist = unserialize($info['cardids']);

foreach ($cardlist as $i => $a) {
    $item[] = $a['id']."|".$a['is_km'];
}
$info['cardlist'] = $item;

$city_id = $_COOKIE['account_id'];

$cityname = $_COOKIE['cityname'];

$yhlist = pdo_fetchall("select * from ims_zhtc_coupons where is_show = 1 and cityname = '".$cityname."'");

$kmlist = pdo_fetchall("select * from ims_zhtc_video_card where city_id = ".$city_id);

if(!empty($yhlist)){
    foreach ($yhlist as $key => $value) {
       $yh['id'] = $value['id'];
       $yh['storename'] = pdo_get('zhtc_store',array('id'=>$value['store_id']))['store_name'];
       $yh['name'] = $value['name'];
       $yh['is_km'] = 0;
       $yh['typename'] = $value['cardtype'] == 0?'超级券':'优惠券';
       $yh['number'] = $value['number'];
       $yhs[] = $yh;
       if ($value['cardtype'] == 0) {
          if (strtotime($value['end_time'] < time())) {
            unset($yhlist[$key]);
          }
       }
    }

}else{
    $yhs = [];
}


if(!empty($kmlist)){
    foreach ($kmlist as $k => $v) {
       $km['id'] = $v['id'];
       $km['storename'] = "自营";
       $km['name'] = $v['title'];
       $km['is_km'] = 1;
       $km['typename'] = "卡密券";
       $km['number'] = pdo_fetchcolumn("select count(*) from ims_zhtc_video_card_data where cid = ".$v['id']." and state = 0");
       $kms[] = $km;
    }

}else{
    $kms = [];
}

$list = array_merge($yhs,$kms);

foreach ($list as $f => $g) {
 
    $str = $g['id']."|".$g['is_km'];
    
       
    if (in_array($str, $info['cardlist'])) {
        $list[$f]['check'] = 1;
    }

}

if(checksubmit('submit')){
     	
        $data['title'] = $_GPC['title'];
        $data['city_id'] = $_COOKIE['account_id'];
        $data['uniacid'] = $_W['uniacid'];
       
       
        foreach ($_GPC['ids'] as $key => $value) {
            $ids = explode("|", $value);
            $array['id'] = $ids[0];
            $array['is_km'] = $ids[1];
            $arrays[] = $array;
        }
        $data['cardids'] = serialize($arrays);
        $data['addtime'] = date('Y-m-d H:i:s');
        
     if($_GPC['id']==''){  
       
       
        $res=pdo_insert('zhtc_qrcode',$data);
        if($res){
             message('添加成功！', $this->createWebUrl('qrcodelist'), 'success');
        }else{
             message('添加失败！','','error');
        }
    }else{
     
        $res=pdo_update('zhtc_qrcode',$data,array('id'=>$_GPC['id']));
        if($res){
             message('编辑成功！', $this->createWebUrl('qrcodelist'), 'success');
        }else{
             message('编辑失败！','','error');
        }
    }
}
include $this->template('web/addcode');